package com.catmiao.rdd.operate.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import scala.Tuple2;

import java.util.Arrays;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_14_reduceByKey {

    public static void main(String[] args) throws InterruptedException {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);


        JavaPairRDD<String, Integer> pairRDD = jsc.parallelizePairs(
                Arrays.asList(
                        new Tuple2<String, Integer>("a", 1),
                        new Tuple2<String, Integer>("b", 2),
                        new Tuple2<String, Integer>("a", 3),
                        new Tuple2<String, Integer>("b", 4)
                )
        );


        /**
         * 将kv类型的数据按照 K 对 V进行reduce操作
         *  [1,3] -> 4
         *  所有的计算都是两两计算
         */
        pairRDD.reduceByKey(Integer::sum).collect().forEach(System.out::println);


        jsc.close();


    }
}
